import { _decorator, Component } from 'cc';
const { ccclass, property } = _decorator;
import * as fgui from "fairygui-cc";
import { forEach } from 'lodash';
import ResultPlayerEntity from '../entity/ResultPlayerEntity';
import { AudioManager } from '../manager/AudioManager';
import { GameController } from '../manager/GameController';
export default class ResultCtrl {

    private winNode: fgui.GComponent = null;
    private failNode: fgui.GComponent = null;

    private controller: fgui.Controller = null;

    private type: string = null;
    private container: fgui.GComponent = null;
    private containerChilds: Array<fgui.GObject> = [];

    public resetHanlde: () => void = null;

    constructor(winNode: fgui.GComponent, failNode: fgui.GComponent, controller: fgui.Controller, bottomY: number) {
        this.winNode = winNode
        this.failNode = failNode
        this.controller = controller

        const win_button = winNode.getChild('n27').asCom;
        const fail_button = failNode.getChild('n27').asCom;
        const win_back = winNode.getChild('n28').asCom;
        const fail_back = failNode.getChild('n28').asCom;

        [win_button, fail_button].forEach((button) => {
            button.y = bottomY
        });

        [win_button, fail_button, win_back, fail_back].forEach((button) => {
            button.onClick(() => {

                this.controller.setSelectedIndex(0)
                this.reset()
                this.resetHanlde();
            })
        })
    }

    public showResult() {

        let resultData = GameController.Instance.gameResultData;
        if (resultData == null) {
            this.reset()
            this.resetHanlde();
            return;
        }
        this.type = resultData.type;

        let node: fgui.GComponent = null;
        if (this.type === 'win') {
            this.controller.setSelectedIndex(1)
            node = this.winNode;

            AudioManager.Instance.playEffectWithName('win')
        }
        else if (this.type === 'fail') {
            this.controller.setSelectedIndex(2)
            node = this.failNode;
            AudioManager.Instance.playEffectWithName('lose')
        }

        const valueLabel: fgui.GComponent = node.getChild('n23', fgui.GComponent);
        valueLabel.text = `${GameController.Instance.gameResultValue}`;

        this.creatResultView(node, resultData.resultArray)
    }

    public creatResultView(node, result: Array<resultInfo>) {

        this.container = node.getChild('container').asCom

        if (this.container) {
            this.container.width = 0;
            this.containerChilds.forEach(item => {
                item.removeFromParent()
                item.dispose()
            })
            this.containerChilds = [];
        }

        console.log(this.container._children)

        let winBg = fgui.UIPackage.createObject("GameBoom", "rt_home");
        let loseBg = fgui.UIPackage.createObject("GameBoom", "lose_bg");

        let winImage = fgui.UIPackage.createObject("GameBoom", "winner");
        let loseImage = fgui.UIPackage.createObject("GameBoom", "lose");

        const spacing = 6;
        const padding = 20;

        const con_width = result.length * (62 + spacing) - spacing + padding * 2

        this.containerAddChild(winBg);
        this.container.width = con_width;
        this.container.x = (node.width - con_width) / 2

        winBg.x = 0; winBg.y = 0;
        winBg.height = this.container.height;
        winBg.width = this.container.width;

        let flagIndex = 1;
        let tempFlag = result.findIndex(item => item.result === false);
        //全输了
        if (tempFlag >= 0) {
            flagIndex = result.length - result.findIndex(item => item.result === false);
        }
        //全赢了
        else {
            flagIndex = 0;
        }

        if (flagIndex != 0) {
            this.containerAddChild(loseBg);
            loseBg.height = winBg.height;
            loseBg.width = padding + flagIndex * (62 + spacing) - spacing / 2
            loseBg.y = 0; loseBg.x = this.container.width - loseBg.width;
        }

        result.forEach((item, idx) => {
            const resultNode = fgui.UIPackage.createObject("GameBoom", "Component6").asCom;
            this.containerAddChild(resultNode);
            const entity = new ResultPlayerEntity(resultNode);
            entity.updateInfo(item)
            resultNode.x = padding + idx * (resultNode.width + spacing)
            resultNode.y = (this.container.height - resultNode.height) / 2
        })

        if (tempFlag < 0) {
            this.containerAddChild(winImage);
            winImage.x = 0; winImage.y = -(winImage.height / 2)
        }
        else if (tempFlag == 0) {
            this.containerAddChild(loseImage);
            loseImage.x = this.container.width - loseImage.width;
            loseImage.y = winImage.y
        }
        else {
            this.containerAddChild(winImage);
            winImage.x = 0; winImage.y = -(winImage.height / 2)
            this.containerAddChild(loseImage);
            loseImage.x = this.container.width - loseImage.width;
            loseImage.y = winImage.y
        }
    }

    private containerAddChild(child) {
        this.container.addChild(child);
        this.containerChilds.push(child);
    }

    public reset() {
        this.controller.setSelectedIndex(0)
    }
}